<?

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/flash/generatepagelist.php
#	FUNCTION:		Generates an XML document of pages in the current site
#	AUTHOR:			Cameron Morrow
#	CREATED:		26/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Report all errors
error_reporting(E_ALL);

# Start session
session_start();

# Use this for IE to remember form data correctly
header("Cache-control: private");

# XML
header("content-type: text/xml");

# Open XML
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";

# Vars
$ERROR_ARRAY = array();
$SQL_ARRAY = array();



# Includes
include "../../site_specific/project.php";
include "../../includes/constants.php";
include "../../includes/utilities.php";
include "../../includes/pages.php";
include "../../includes/database.php";
include "../../includes/users.php";
include "../../includes/files.php";

# Establish database connection
establishDatabaseConnection();

# Get pages
$pages = getRecord("SELECT p_id, p_name, p_parent, p_section, p_sort FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_section != 'admin' ORDER BY p_sort ASC, p_name ASC");

# Page to be selected
$select = @$_GET["select"] ? $_GET["select"] : "";

# Get path to selected page
$select_path = getPathToPage($select);

# Page ID generator
$page_id_num = 1;


echo "<list name=\"Pages\" foldericon=\"tree/folder.png\" fileicon=\"tree/file.png\" show=\"1\">\n";
printPage("");
echo "</list>";

/**
 * printPage() prints a page and it's children. It's a recursive function
 *
 * @param print_page is the ID of the page to print
*/
function printPage($print_page) {

	global $pages;
	global $select_path;
	global $page_id_num;

	# Get page name, sort order and children pages
	$print_page_name = "";
	$print_page_sort = "";
	$children_pages = array();

	# Unsorted pages

	# For each page
	foreach ($pages as $page_id => $page) {

		if ($page["p_id"] == $print_page) {
			$print_page_name = $page["p_name"];
			$print_page_sort = $page["p_sort"];

		} else if ($page["p_parent"] == $print_page) {
			$children_pages[] = $page;
		}
	}

	# Expanded?
	$expanded = array_search($print_page, $select_path) !== false;

	echo $print_page != "" ? "<item id=\"" . $page_id_num . "\" pageid=\"" . $print_page . "\" name=\"" . $print_page_name . "\" sort=\"" . $print_page_sort . "\" expanded=\"" . ($expanded ? "1" : "0") . "\">\n" : "";
	$page_id_num++;
	foreach ($children_pages as $page) {
		printPage($page["p_id"]);
	}
	echo $print_page != "" ? "</item>" : "";
}

function getPathToPage($page_id) {
	$path = array();

	while (getParentOfPage($page_id) !== false) {
		$parent = getParentOfPage($page_id);
		$path[] = $parent;
		$page_id = $parent;
	}

	array_pop($path);
	return $path;
}

function getParentOfPage($page_id) {
	global $pages;
	foreach ($pages as $page) {
		if ($page["p_id"] == $page_id) {
			return $page["p_parent"];
		}
	}
	return false;
}

?>